---------------------------------------------------------------
-- @Copyright：           zour
-- @Description：         调用账号服的接口函数

-- @modify time           2021-09-27
---------------------------------------------------------------
require "modules.business_advert.advert_define"

local FOO = {
	_VERSION = '0.1.0',

	__INT32__  = 2147483648;
	__UINT32__ = 4294967296;
}

--===================================local function=====================================
local function call_account(url_, params_)
	log.debug( "call_account url=" .. url_ )
	local url_str_ = ""
	for k, v in pairs(params_) do
		url_str_ = url_str_ .. k .. '=' .. ngx.escape_uri(v) .. '&'
	end

	local md5_seed_ = uu.getStringBySortKey( params_ ) .. '522e6ccde68f4584a3282ce0b0eca1cf'

	local url_all_ = url_ .. '?' .. url_str_ .. 'sign=' .. ngx.md5( md5_seed_ )

	local ret, body = ns_network.getHttpPage( url_all_ )

	if  ret == 200 then
		return uu.json2table( body )
	else
		return {}
	end
end

--===================================api function=====================================
--测试调用账号服接口
--[[
FOO.test_account_api = function()
	--获取所有物品
	local ret, result = FOO.get_all_items()
	if ret then 
		log.debug("-------------------test_account_api get_all_items ok!")
		uu.var_dump(result)
	end
	--查看迷你点抽奖券数量
	local ret, result = FOO.get_item_nums(10054)
	if ret then 
		log.debug("-------------------test_account_api get_item_nums ok! nums= "..result)
	end
	--增加迷你点抽奖券数量
	local items = {
		{id=10054, num=1}
	}
	local ret = FOO.items_add(items)
	if ret then 
		local ret, result = FOO.get_item_nums(10054)
		log.debug("-------------------test_account_api items_add ok!  nums= "..result)
	end
	--增加迷你点抽奖券数量
	local ret = FOO.item_reduce(10054, 1)
	if ret then 
		local ret, result = FOO.get_item_nums(10054)
		log.debug("-------------------test_account_api item_reduce ok!  nums= "..result)
	end
end
--]]

--! @brief 减少物品
FOO.item_reduce = function(itemid, num, reason_)
	local itemid_ = tonumber(itemid)
	local num_ = tonumber(num) or 0
	local reason_ = reason_ or "business_advert_exchange"
	local info = {
		cmd = 'item_rm',
		id = itemid_,
		num = num_, 
		reason = reason_,
		uin = ngx.ctx.m_params.uin,
		ts = uu.now(),
	 }
	local ret = call_account( ns_config.gm_cost_server_url[ (ngx.ctx.m_params.uin  % #ns_config.gm_cost_server_url) + 1 ],info)
	if ret.return_code == 'SUCCESS' then
		log.day_list(ADVERT, "item_remove",ngx.ctx.m_params.uin,itemid, num,reason_,uu.table2json(ret.return_code), uu.now())
		return true
	else
		log.day_list(ADVERT, "item_remove",ngx.ctx.m_params.uin,itemid, num,reason_,uu.table2json(ret.return_code), uu.now())
		return false
	end
end

--! @brief 增加物品 items: {{id=xx,num=xx},{id=xx,num=xx},...}
FOO.items_add = function(items, reason_)
	local new_items = {}
	for _, item in pairs(items) do 
		local id = tonumber(item.id) or 0
		local num = tonumber(item.num) or 0
		if id and id > 0 and num and num > 0 then 
			new_items[#new_items+1] = {id=id, num=num}
		else
			log.day_list(ADVERT, "items_add", "id or num is nil", ngx.ctx.m_params.uin, reason_, id,num)
		end
	end
	local reasons =  reason_ or 'business_advert_exchange'
	local json_item = uu.table2json(new_items)
	local info = {
		cmd ='items_add', 
		items = json_item, 
		reason = reasons,
		uin      = ngx.ctx.m_params.uin,
		ts       = uu.now(),
	}
	local ret = call_account( ns_config.gm_cost_server_url[ (ngx.ctx.m_params.uin % #ns_config.gm_cost_server_url) + 1 ],info)
	if ret.return_code == 'SUCCESS' then
		log.day_list(ADVERT, "items_add","success",ngx.ctx.m_params.uin,reason_,json_item,uu.table2json(ret),uu.now())
		return true
	else
		log.day_list(ADVERT, "items_add","fail",ngx.ctx.m_params.uin,reason_,json_item,uu.table2json(ret),uu.now())
		return false
	end
	return false
end

--! @brief 获取账号服物品信息
FOO.get_all_items = function(uin)
	uin = uin or ngx.ctx.m_params.uin
	--net
	local tmp_ = {
		cmd      = "query",
		path     = "Account.ItemInfo",
		uin      = uin or 0,
		ts       = uu.now(),
		-- ignore   = 1,
	};
	local ret_ = call_account( ns_config.gm_cost_server_url[ (uin % #ns_config.gm_cost_server_url) + 1 ], tmp_ );
	if ret_.return_code and ret_.return_code=='SUCCESS' then
		return true, ret_.return_result or {};
	else
		log.day_list(ADVERT, 'account', "get_account_items_info fail", uin, uu.to_str(ret_) );
		return false;
	end
end

--! @brief 获取账号服某个道具数量
FOO.get_item_nums = function(id, uin)
	uin = tonumber(uin) or tonumber(ngx.ctx.m_params.uin)
	id = tonumber(id) or 0
	if uin <= 0 or id <= 0 then 
		log.day_list(ADVERT, "account", "get_account_item_num fail. uin or id is nil", uin, id);
		return false
	end
	local ret, v = FOO.get_all_items(uin)
	if not ret then 
		log.day_list(ADVERT, "account", "get_account_item_num fail", uin, id, uu.to_str(ret) );
		return false
	end
	-- [3]={["Num"]=1026,["ItemID"]=20032}
	if v['Account.ItemInfo'] then 
		for _, item in ipairs(v['Account.ItemInfo']) do
			if item.ItemID == id then 
				return true, item.Num
			end
		end
	end
	return true, 0
end

return FOO
